import smtplib
import ssl
from email.mime.text import MIMEText


def send_email():
    # 配置信息（替换为实际值）
    SMTP_SERVER = "smtp.qq.com"  # 如 smtp.gmail.com
    PORT = 465  # 或 465（SSL）587
    SENDER_EMAIL = "949819874@qq.com"
    PASSWORD = "ieqakeyxxnhtbeec"  # 注意：使用授权码而非登录密码
    RECEIVER_EMAIL = "tttzzzqqq@qq.com"

    # 创建邮件内容
    message = MIMEText("这是邮件正文12341234")
    message["Subject"] = "测试邮件002"
    message["From"] = SENDER_EMAIL
    message["To"] = RECEIVER_EMAIL

    try:
        # 创建安全连接
        context = ssl.create_default_context()

        # 根据端口选择连接方式
        if PORT == 465:
            with smtplib.SMTP_SSL(SMTP_SERVER, PORT, context=context) as server:
                server.login(SENDER_EMAIL, PASSWORD)
                server.sendmail(SENDER_EMAIL, RECEIVER_EMAIL, message.as_string())
        else:
            with smtplib.SMTP(SMTP_SERVER, PORT) as server:
                server.starttls(context=context)  # 启用加密
                server.login(SENDER_EMAIL, PASSWORD)
                server.sendmail(SENDER_EMAIL, RECEIVER_EMAIL, message.as_string())

        print("邮件发送成功！")

    except smtplib.SMTPException as e:
        print(f"邮件发送失败: {e}")


# 执行发送
send_email()
